{{ $link := .Destination }}
{{ $isRemote := strings.HasPrefix $link "http" }}
{{ if not $isRemote }}
  {{ $url := urls.Parse .Destination }}
  {{ $path := replace $url.Path "README" "" }}
  {{ $path = replace $path ".md" "" }}
  {{ $url = urls.Parse $path }}
  {{ if or ($url.Path) (eq .Destination "README.md") }}
    {{ $fragment := "" }}
    {{ with $url.Fragment }}
      {{ $fragment = printf "#%s" . }}
    {{ end }}
    {{ $link = $url.Path }}
    {{ with .Page.GetPage $url.Path }}
      {{ $link = printf "%s%s" .RelPermalink $fragment }}
    {{ end }}
  {{ end }}
{{ end }}
<a href="{{ $link | safeURL }}"{{ with .Title }} title="{{ . }}"{{ end }}{{ if $isRemote }} target="_blank"{{ end }}>{{ .Text | safeHTML }}</a>
